<?php
// +----------------------------------------------------------------------
// | Author: 发仔 <i@fazi.me> 
// +----------------------------------------------------------------------

namespace app\admin\controller;

use think\Db;

class Order Extends Base
{
	protected $id = 'order_id';
	protected $state = 'order_state';
	protected $table = 'order';
	protected $joins = [['car c','c.car_id = m.car_id','left'],['driver d','d.driver_id = m.driver_id','left'],['car_type ct','ct.car_type_id = c.car_type_id','left'],['order_fee of','of.order_id = m.order_id','left']];
	protected $field = 'm.*,of.*,c.register_time,ct.car_type_name,ct.type,c.number,d.name AS driver_name,d.phone AS driver_phone';
	
	//列表
    public function index() {
		
		$order_start = input('param.order_start');
		$order_end = input('param.order_end');
		
		$apply_start = input('param.apply_start');
		$apply_end = input('param.apply_end');
		
		$where = $this->search();
		
		$order_start = $order_start ?: '1970-01-01';
		$order_end = $order_end ?: date('Y-m-d');
		$apply_start = $apply_start ?: '1970-01-01';
		$apply_end = $apply_end ?: date('Y-m-d');
		
		$list = $this->joins()->where($where)->where('orders_time','>=',$order_start)->where('orders_time','<=',$order_end)->whereNull('m.delete_time')->order('m.'.$this->id.' DESC')->fetchSql(0)->paginate(10,false,[
		'query' => input('param.')
		]);
//		halt($list);
		$this->assign('list',$list);
		$this->assign('pager',$list->render());
		
		return $this->out();
		
	}
	
	//搜索
	protected function search() {
		//搜索
		$where = [];
		$P = input('param.');
		$P['order_no'] && $where['order_no'] = ['like', '%'.$P['order_no'].'%'];
		$P['passenger_name'] && $where['m.passenger_name'] = ['like', '%'.$P['passenger_name'].'%'];
		$P['passenger_phone'] && $where['m.passenger_phone'] = ['like', '%'.$P['passenger_phone'].'%'];
		$P['order_type'] && $where['order_type'] = $P['order_type'];
		$P['order_state'] && $where['order_state'] = $P['order_state'];
		
		//公务只能看公务订单
		if( $this->user['official'] ){
			$where['order_type'] = 1;
//			$where['ct.type'] = ['in','2,3'];
		}
		//乘客
		if($P['car_use_id']) {
			$this->joins[] = ['passenger p','p.passenger_id = m.passenger_id'];
			$this->joins[] = ['car_use cu','p.car_use_id = cu.car_use_id'];
			$where['cu.car_use_id'] = $P['car_use_id'];
		} 
		
		$this->options($where);
		
		//弹出层搜索
		$op = input('param.op');
		if(in_array($op, ['search','bind','unbind']) ) {
			$this->assign('notPopup',0);
		}
		$this->assign('op',$op);
		
		return $where;
		
	}
	
	public function detail() {
		
		$this->field .= ',cu.name AS car_use_name';
		$this->joins[] = ['passenger p','p.passenger_id = m.passenger_id','left'];
		$this->joins[] = ['car_use cu','p.car_use_id = p.car_use_id','left'];
		
		$id = input('param.'.$this->id);
		$item = $this->joins()->where('m.'.$this->id,$id)->find();
		
//		halt($item);
		
		$this->assign('item',$item);
		return $this->out();
//		halt($this->joins);
		
	}
	
	//派车
	public function dispatch() {
		
		return $this->detail();
	}
	
	public function stat() {
		
		
		$start = input('param.start','1970-01-01');
		$end = input('param.end',date('Y-m-d'));
		$time = input('param.time','year');
		
		$start = $start ?: '1970-01-01';
		$end = $end ?: date('Y-m-d');
		
		if($time == 'day') {
			$start = date('Y-m-d');
		} elseif($time == 'month') {
			$start = date('Y-m-01');
		} elseif($time == 'year') {
			$start = date('Y-01-01');
		}
		
		$types = config('base.use_types');
		
		//所有订单分析
		$list = Db::name('order')->alias('o')->join('order_fee of','of.order_id = o.order_id')->where('order_time','>=',$start)->where('order_time','<=',$end)->fetchSql(0)->select();
		
		$qty = $price = $time = [];
		
//		halt(array_column($list,'use_type'));
		
		foreach($list AS $order) {
			
			//订单数
			$qty[$order['use_type']]++;
			$price[$order['use_type']] += $order['order_money'];
			$time[$order['use_type']] += $order['time'];
			
		}
		
		$this->assign('types',$types);		
		$this->assign('qty',$qty);		
		$this->assign('price',$price);		
		$this->assign('time',$time);		
		
		return $this->out();
	}
	
	protected function options($item = []) {
		
		//公务只能看公务订单
		$order_types = config('base.order_types');
		if( $this->user['official'] ){
			$order_types = false;
		}
		
		$this->assign('order_types',$order_types);
		return $item;
	}

    /**
     * 获取轨迹
     * @return json
     */
	public function getTrack(){
        $oid = input('oid',0);
        return (new \app\api\model\Order())->getTrack($oid);
    }
	
}
